/**
 * 
 */
package name.webdizz.gadget.four.envelope.client.view;

import name.webdizz.gadget.four.envelope.client.locale.Locale;
import name.webdizz.gadget.four.envelope.client.locale.LocaleFactory;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

/**
 * @author Izzet_Mustafayev
 * 
 */
public class LoginView extends Composite implements ILoginView {

	private Widget loginFormPanel;

	private Button submitButton;

	private TextBox usernameTextBox;

	private PasswordTextBox passwordTextBox;

	private static final Locale locale = LocaleFactory.instance();

	/**
	 * Creates {@link LoginView} form.
	 */
	public LoginView() {
		super();
		loginFormPanel = createLoginFormPanel();
		initWidget(loginFormPanel);
	}

	/**
	 * Create a form that contains undisclosed advanced options.
	 */
	private Widget createLoginFormPanel() {
		// Create a table to layout the form options
		FlexTable layout = new FlexTable();
		layout.setCellSpacing(6);
		layout.setWidth("200px");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// Add a title to the form
		layout.setHTML(0, 0, locale.enterCredentials());
		cellFormatter.setColSpan(0, 0, 2);
		cellFormatter.setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		// Add some standard form options
		layout.setHTML(1, 0, locale.username());
		usernameTextBox = new TextBox();
		usernameTextBox.setWidth("100px");
		layout.setWidget(1, 1, usernameTextBox);
		layout.setHTML(2, 0, locale.password());
		passwordTextBox = new PasswordTextBox();
		passwordTextBox.setWidth("100px");
		layout.setWidget(2, 1, passwordTextBox);

		submitButton = new Button(locale.save());
		layout.setWidget(3, 1, submitButton);

		// Wrap the contents in a DecoratorPanel
		DecoratorPanel decPanel = new DecoratorPanel();
		decPanel.setWidget(layout);
		decPanel.setWidth("80%");
		return decPanel;
	}

	/**
	 * @return the usernameTextBox
	 */
	public TextBox getUsernameTextBox() {
		return usernameTextBox;
	}

	/**
	 * @return the passwordTextBox
	 */
	public PasswordTextBox getPasswordTextBox() {
		return passwordTextBox;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * name.webdizz.gadget.four.envelope.client.view.ILoginView#addSubmitHandler
	 * (com.google.gwt.event.dom.client.ClickHandler)
	 */
	public void addSubmitHandler(final ClickHandler clickHandler) {
		submitButton.addClickHandler(clickHandler);
	}

}
